<?php

namespace App\Console\Commands;

use App\Models\Article;
use App\Jobs\RobotMessageJob;
use Illuminate\Support\Carbon;
use Illuminate\Console\Command;

class RobotMessage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'robot:message';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '发送机器人消息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * current 立即发送
     * @return int
     */
    public function handle()
    {
        $article = Article::where('status',0)
            ->select([
                'id',
                'type',
                'time'
            ])
            ->orderBy('created_at','asc')
            ->get()
            ->toArray();

        foreach ($article as $value) {
            switch ($value['type']) {
                case 'at_current':
                    $this->currentSend($value);
                    break;
                case 'at_date':
                    $this->dateSend($value);
                    break;
                case 'every_day':
                    $this->everyDay($value);
                    break;
                case 'every_week':
                    $this->everyWeek($value);
                    break;
                case 'every_month':
                    $this->everyMonth($value);
            }
        }
    }

    /**
     * 绑定job数据
     * @param int $articleId
     */
    public function send(int $articleId)
    {
        RobotMessageJob::dispatch($articleId)
            ->onQueue('robot_message')
            ->onConnection('redis');
    }

    /**
     * 立即发送
     * @param array $data
     */
    public function currentSend(array $data)
    {
        $this->send($data['id']);
    }

    /**
     * 在某个时间点进行发送
     * 采用时间戳进行判断
     * @param array $data
     */
    public function dateSend(array $data)
    {
        if (Carbon::parse($data['time'])->timestamp <= Carbon::now()->timestamp) {
            $this->send($data['id']);
        }
    }

    /**
     * 每天的某个时间进行判断
     * @param array $data
     */
    public function everyDay(array $data)
    {
        list($hour,$minute) = explode(":",$data['time']);

        $date = Carbon::now();

        if ( $date->hour == $hour && $date->minute == $minute) {
            $this->send($data['id']);
        }
    }

    /**
     * 每周的第几天
     * @param array $data
     */
    public function everyWeek(array $data)
    {
        $date = Carbon::now();

        list($week,$hour,$minute) = explode(":",$data['time']);

        //星期天等于0
        $week == 7 ? $week = 0 : $week;

        if ($date->dayOfWeek == $week) {
            if ( $date->hour == $hour && $date->minute == $minute) {
                $this->send($data['id']);
            }
        }
    }

    /**
     * 每个月第几天
     * @param array $data
     */
    public function everyMonth(array $data)
    {
        $date = Carbon::now();

        list($day,$hour,$minute) = explode(":",$data['time']);

        if ($date->day == $day) {
            if ( $date->hour == $hour && $date->minute == $minute) {
                $this->send($data['id']);
            }
        }
    }
}
